<?php
/*
 * 按照phpcms图片模型的指定格式处理图片地址
 */
$dir = "G:/To";
$mydir = dir($dir); 
$sqlFile = fopen("$dir/pic_data.sql", 'w') or dir("");
while($file = $mydir->read())
{ 
	if(is_dir("$dir/$file"))
		continue;
	if(strpos($file, '.json') < 0)
		continue;
	$id = substr($file, 0, strpos($file, '.json')) + 1;
	echo $id . "</br>";
	$filename = "$dir/$file";
	$myfile = fopen($filename, 'r') or die("Unable Open file: $dir/$file");	
	$json = fread($myfile, filesize($filename));
	fclose($myfile);
	// 解析json数据
	$arr = json_decode($json);
	$arr_res = $temp = array();
	foreach ($arr as $key => $value) {
		$temp['url'] = $value->url;
		$temp['alt'] = $value->alt;
		$arr_res[$key] = $temp;
	}
	$res_str = array2string($arr_res, 0);
	$sql = "INSERT INTO `shenyou`.`sy_pic_data` (`id`, `pictureurls`, `copyfrom`) VALUES ('$id','$res_str', '|0');";
	fwrite($sqlFile, $sql."\r\n");

} 
fclose($sqlFile);
$mydir->close(); 

function array2string($data, $isformdata = 1) {
	if($data == '') return '';
	if($isformdata) $data = new_stripslashes($data);
	return addslashes(var_export($data, TRUE));
}

function new_stripslashes($string) {
	if(!is_array($string)) return stripslashes($string);
	foreach($string as $key => $val) $string[$key] = new_stripslashes($val);
	return $string;
}

?>
